package com.aliyun.messagemgt.service;

import com.alibaba.fastjson.JSON;
import com.aliyun.messagemgt.api.AsyncPushAPIHttp;
import com.aliyun.messagemgt.model.AsyncPushRequest;
import com.aliyun.messagemgt.model.AsyncPushResponse;
import com.aliyun.messagemgt.rocketmq.MessageInnerForwardMQTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Qinyuan
 * @date 2025/7/29
 */
@RestController
@Slf4j
public class AsyncPushController implements AsyncPushAPIHttp {

    @Autowired
    private MessageInnerForwardMQTemplate messageInnerForwardMQTemplate;

    @Override
    public ResponseEntity<AsyncPushResponse> msgPush(AsyncPushRequest request) {
        AsyncPushResponse response = new AsyncPushResponse();
        if (StringUtils.isBlank(request.getTenantCode()) || StringUtils.isBlank(request.getSecretKey())) {
            response.setSuccess(false);
            response.setErrMsg("tenantCode and secretKey can not be null");
            return ResponseEntity.ok(response);
        }
        try {
            messageInnerForwardMQTemplate.asyncSend(request);
            response.setSuccess(true);
        } catch (Exception e) {
            log.error("AsyncPushService msgPush error, request:{}", JSON.toJSONString(request), e);
        }
        return ResponseEntity.ok(response);
    }
}
